package com.sskj.login.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.WidgetAccess;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.LanguageUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.box.verifier.ToastNextInputs;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.EditUtil;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.login.R;
import com.sskj.login.R2;
import com.sskj.login.bean.LoginBean;
import com.sskj.login.bean.rxbus.AccountBean;
import com.sskj.login.component.DaggerUserDataComponent;
import com.sskj.login.presenter.LoginActivityPresenter;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.LOGIN_LOGIN)//登录
public class LoginActivity extends BaseActivity<LoginActivityPresenter> {
    @BindView(R2.id.tvRegister)
    TextView tvRegister;
    @BindView(R2.id.etAccount)
    EditText etAccount;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.btLogin)
    Button btLogin;
    @BindView(R2.id.tvPwdForget)
    TextView tvPwdForget;
    @BindView(R2.id.ivPwdToggle)
    ImageView ivPwdToggle;
    private AndroidNextInputs inputs;
    @Inject
    UserViewModel userViewModel;
    private BottomSheetDialog googleCheckDialog;

    private WidgetAccess access;
    private BottomSheetDialog priceModeSheet;
    private int type = 1;

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.login_activity_login;
    }

    @Override
    public LoginActivityPresenter getPresenter() {
        return new LoginActivityPresenter();
    }

    @Override
    protected void initView() {
        RxBus.getDefault().send(RxBusCode.LOGOUT);
        DaggerUserDataComponent.create().inject(this);
        RxBus.getDefault().register(this);
        ClickUtil.click(tvRegister, () -> {
            ARouter.getInstance().build(RConfig.LOGIN_REGISTER).navigation();
        });
        EditUtil.togglePs(etPwd, ivPwdToggle);


        ClickUtil.click(btLogin, () -> {

            inputs.clear();
            inputs
                    .add(access.findEditText(R.id.etAccount), SchemeUtil.notEmpty(etAccount), SchemeUtil.phone())
                    .add(access.findEditText(R.id.etPwd), SchemeUtil.notEmpty(etPwd), SchemeUtil.pwd())
            ;
            if (TextUtils.isEmpty(etAccount.getText().toString())) {

                ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity1));

                return;
            }
            mPresenter.login(null, etAccount.getText().toString(), etPwd.getText().toString(), null, null);
        });


        ClickUtil.click(tvPwdForget, () -> {
            ARouter.getInstance().build(RConfig.LOGIN_PWD_FORGET).navigation();
        });

        inputs = new ToastNextInputs();
        access = new WidgetAccess(this);
        LiveDataBus.get()
                .with(RxBusCode.REGISTER_ACCOUNT, AccountBean.class)
                .observe(this, accountBean -> {
                    if (etAccount != null && accountBean != null) {
                        if (accountBean.getType() == type) {
                            etAccount.setText(accountBean.getAccount());
                        } else {
                            etAccount.setText("");
                        }
                    } else {
                        etAccount.setText("");
                    }
                });
    }

    public void loginSuccess(LoginBean data) {

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(SPConfig.USER_ACCOUNT, etAccount.getText().toString());
        httpHeaders.put(SPConfig.TOKEN, data.getToken());
        httpHeaders.put("locale", LanguageUtil.getAppLocale(App.INSTANCE).toString().replace("_","-"));
        SPUtil.put(SPConfig.USER_ACCOUNT, etAccount.getText().toString());
        SPUtil.put(SPConfig.TOKEN, data.getToken());
        OkGo.getInstance().addCommonHeaders(new HttpHeaders());
        OkGo.getInstance().getCommonHeaders().clear();

        OkGo.getInstance().addCommonHeaders(httpHeaders);
        userViewModel.update();
        finish();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);

    }

    public void googleCheck(String secret) {

        googleCheckDialog = BottomSheetUtil.showCheck(this, false, true, false, str -> {
            googleCheckDialog.dismiss();
            mPresenter.login("86", etAccount.getText().toString(), etPwd.getText().toString(), str[1], secret);

        });
        googleCheckDialog.show();

    }

    @Override
    protected void onDestroy() {
        RxBus.getDefault().unregister(this);

        super.onDestroy();
    }
}
